package com.runa.monitor.platinterface.netservice.equipment;

import com.runa.monitor.comm.dto.Archive;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.persistence.util.SpringContextUtil;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.monitor.platinterface.platService.RepositoryService;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import com.runa.rccprotocol.dto.thtf.THTFRequestData;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
public class THTFDriver extends NetEqu<String> {

    public THTFDriver(String serial, Integer timeout) {
        super(serial, timeout);
    }

    @Override
    public void readNodeEqu(List<Node> nodeList, TaskFunction function, EquData.EquDataType type) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        switch (type) {
            case VALVE:
            case PUBLIC_UNIT:
                readHouseValve(nodeList, function);
                break;
            default:
        }
    }

    private void readHouseValve(List<Node> hValve, TaskFunction function) {
        RepositoryService repositoryService = SpringContextUtil.getBean(RepositoryService.class);
        for (Node node : hValve) {
            THTFRequestData requestData = new THTFRequestData(repositoryService.getCommNetEquipmentByNetSerial(serial()).getMac());
            requestData.readValve(node.getSerial());
            requestData.setRemain(Collections.singletonList(node));
            sendRequest(requestData, function);
        }
    }

    public static void main(String[] args) {
        THTFRequestData requestData = new THTFRequestData("7A728605556D");
//        requestData.readValve("92EEEAC31F74");
//        requestData.houseValveOpen("92EEEAC31F74", 60);
        requestData.houseValveOpen("CCB8846703F9", 60);
        String s = ByteUtil.byteToHexString(requestData.getData());
        System.out.println(s);
    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void valveOnOffControl(List<Node> valveSerial, boolean ifLock, Integer opening, TaskFunction function) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, valveSerial);
            return;
        }
        RepositoryService repositoryService = SpringContextUtil.getBean(RepositoryService.class);
        for (Node valve : valveSerial) {
            if (valve != null) {
                THTFRequestData requestData = new THTFRequestData(repositoryService.getCommNetEquipmentByNetSerial(serial()).getMac());
                requestData.houseValveOpen(valve.getSerial(), opening);
                requestData.setRemain(valve);
                sendRequest(requestData, function);
            }
        }
    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> valveSerial, Integer model, Map param, TaskFunction function) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, valveSerial);
            return;
        }
        RepositoryService repositoryService = SpringContextUtil.getBean(RepositoryService.class);
        THTFRequestData requestData;
        for (Node unit : valveSerial) {
            if (unit != null) {
                switch (model) {
                    case 1:
                        requestData = new THTFRequestData(repositoryService.getCommNetEquipmentByNetSerial(serial()).getMac());
                        requestData.houseValveOpen(unit.getSerial(), (int) Float.parseFloat(param.get("targetValue").toString()));
                        requestData.setRemain(unit);
                        sendRequest(requestData, function);
                        break;
                    case 2:
                    case 3:
                    case 4:
                        break;
                    default:
                }
            }
        }
    }

    @Override
    public void sentPublicUnitModel(List<Node> valveSerial, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valveSerial, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valveSerial, Node meterSerial, TaskFunction function) {

    }

}
